import { TaskBuild } from './impl/build';
import { TaskClaim } from './impl/claim';
import { TaskDismantle } from './impl/dismantle';
import { TaskFortify } from './impl/fortify';
import { TaskGetBoosted } from './impl/getBoosted';
import { TaskGetRenewed } from './impl/getRenewed';
import { TaskHarvest } from './impl/harvest';
import { TaskInvalid } from './impl/invalid';
import { TaskRepair } from './impl/repair';
import { TaskReserve } from './impl/reserve';
import { TaskSign } from './impl/sign';
import { TaskTransfer } from './impl/transfer';
import { TaskTransferAll } from './impl/transferAll';
import { TaskUpgrade } from './impl/upgrade';
import { TaskWithdraw } from './impl/withdraw';
import { TaskWithdrawAll } from './impl/withdrawAll';
import { TaskTypes } from './impl/types';
import { TaskPickup } from './impl/pickup';
import { TaskBase } from './TaskBase';

type TaskConstructorType = new (proto?: SerializedTaskObject) => TaskBase;
export const TaskTypeMap: Record<string, TaskConstructorType> = {
    [TaskTypes.build]: TaskBuild,
    [TaskTypes.claim]: TaskClaim,
    [TaskTypes.dismantle]: TaskDismantle,
    [TaskTypes.fortify]: TaskFortify,
    [TaskTypes.getBoosted]: TaskGetBoosted,
    [TaskTypes.getRenewed]: TaskGetRenewed,
    [TaskTypes.harvest]: TaskHarvest,
    [TaskTypes.pickup]: TaskPickup,
    [TaskTypes.repair]: TaskRepair,
    [TaskTypes.reserve]: TaskReserve,
    [TaskTypes.sign]: TaskSign,
    [TaskTypes.transfer]: TaskTransfer,
    [TaskTypes.transferAll]: TaskTransferAll,
    [TaskTypes.upgrade]: TaskUpgrade,
    [TaskTypes.withdraw]: TaskWithdraw,
    [TaskTypes.withdrawAll]: TaskWithdrawAll,
};

export function deserializeTask(proto: SerializedTaskObject) {
    // 根据 `proto.name` 实例化成具体的类
    try {
        const Clazz = TaskTypeMap[proto.name];
        if (!Clazz) {
            return new TaskInvalid(proto);
        }
        return new Clazz(proto);
    }
    catch (e) {
        return undefined;
    }
}